

(function(){
    var dataloader = {
        init:function(url){

            var _this = this;

            this.isLoaded = false; //所有数据是否已经加载完
            this.isLoadedOnce = false;//本次数据是否加载完
            this.bottom = 0; //距底部多少就可以继续加载
            this.first = true;
            this.pageIndex = 1;

            this.url = url.url;
            this.data = url.data;
            this.list = url.objData;
            this.historyData = this.data.pageSize;
            this.box = url.content;
            this.temp = url.temp;
            this.attr = url.attList;
            this.fn = url.fn;
            this.history = url.history;
            this.argumList = {};
            this.noData = typeof(url.noData) == "function" ? url.noData() : false;
            //this.argumentList = url.argumentList;

            this.loadingIcon = url.loading && url.loading.icon ? url.loading.icon : "http://cdn.xsmore.com/Images/Common/loading.gif";
            this.loadingTxt = url.loading && url.loading.txt ? url.loading.txt : "加载中...";

            this.currentIndex = url.history ? url.history + "index" : false;
            this.currentCount = url.history ? url.history + "count" : false;
            this.currentTop = 0;

            //初始化标签
            this.initHTML();

            
            if (this.history && this.getHistory("isJump") && this.getHistory("isJump") == "1"){

                this.data = JSON.parse(this.getHistory("data") || "");

                this.data.pageSize = parseInt(this.getHistory("Count") || 10);
                this.data.pageIndex = 1;

                $("body").css("min-height", (parseInt(_this.getHistory("Top")) + $(window).height()) + "px");
                
                this.GetList(1, function(){
                    //console.log(_this.historyData);
                    document.body.scrollTop = parseInt(_this.getHistory("Top"));
                    _this.data.pageIndex = parseInt(_this.getHistory("Index") || 1);
                    _this.data.pageSize = _this.historyData;
                    _this.bind();
                    _this.clearHistory();

                    setTimeout(function(){
                        document.body.scrollTop += 1; 
                    },500)
                })

            }else{
                this.bind();
                this.GetList(1);

            }
            
            return this;
        },
        bind:function(){
            var _this = this;
            //绑定滚动事件
            $(".tips-conver").on("scroll", function () {
                win = $(this);

                var scrollTop = win.scrollTop(),
                    winH = win.height(),
                    docHeight = $(".pro-list").height();

                _this.currentTop = scrollTop;

            
                if (_this.isLoaded) return;

                if (scrollTop + winH + _this.bottom >= docHeight) {
                    _this.data.pageIndex ++ 
                    _this.GetList(_this.data.pageIndex);
                }
            })

        },
        initHTML:function(){
            var link = document.createElement("link");
            link.href = "http://cdn.xsmore.com/CSS/Shop/m_balist.css?v=2015092301";
            link.rel = "stylesheet";
            $("head").append(link);

            var html = '<div class="dt-loading" >'+
                            '<img src="'+this.loadingIcon+'" />'+this.loadingTxt+
                        '</div>'+
                        '<div class="loadNone" style="text-align: center; display: none">'+
                            '<p>已加载完毕</p>'+
                        '</div>'+
                        '<div class="no_record noneData" style="display:none">'+
                            (this.noData || '<img src="http://cdn.xsmore.com/Images/Common/no_record.jpg" />'+
                            '<p>近期无记录</p>')+
                        '</div>';

            this.box.after(html);

            $(".dt-loading").hide();
            $(".loadNone").hide();
            $(".noneData").hide();
        },
        getHistory:function(name){
            return localStorage[this.history + name] || false;
        },
        setHistory:function(name,str){
            localStorage[this.history + name] = str;
        },
        setAllHistory:function(){
            this.setHistory("Top", document.body.scrollTop);
            this.setHistory("Index", this.data.pageIndex);
            this.setHistory("Count", this.currentCount);
            this.setHistory("isJump", "1");

            // if (this.argumentList){
            //     // for (var elem in this.argumList){
            //     //     this.setHistory(elem, this.argumList[elem]);
            //     //     console.log(this.argumList[elem]);
            //     // }
            //     for (var i=0; i<this.argumentList.length; i++){
            //         this.setHistory(this.argumentList[i], this.argumList[this.argumentList[i]] || "");
            //     }
            // }

            this.setHistory("data", JSON.stringify(this.data));
            
        },
        clearHistory:function(){
            this.setHistory("isJump", "0");
        },
        templete:function(){
            return '<div class="cnt_box" style="height:80px">' +
                        '<div class="cnt" style="height:60px">' +
                            '<img style="top:12px;height:55px;width:55px" src="{img}">' +
                            '<div class="cnt_c clearfix" style="padding-left:65px">' +
                                '<div class="left" style="height:60px">' +
                                    '<p class="name">{name}</p>' +
                                    '<p class="time">{date}</p>' +
                                '</div>' +
                                '<div class="right"  style="height:60px">' +
                                    '<p class="tip" idplus="{idplus}" style="margin-top:35px;margin-right:20px;width:100%;height:24px;line-height:24px;background:rgb(200,150,51);text-align:center;color:#fff;border-radius:4px">删除</p>' +
                                '</div>' +
                            '</div>' +
                        '</div>' +
                    '</div>';
        },
        //数据处理
        GetList:function(pageIndex, func){

            var _this = this;

            hide();
            //_this.isLoadedOnce = true;
            $(".dt-loading").show();

            $.ajax({
                url: _this.url,
                data: _this.data || "",
                dataType: "json",
                success:function(data){
                    $(".dt-loading").hide();

                    var list = (_this.list && _this.list(data)) || data.list || false;

                    if (list && list.length){
                        
                        var html = "";
                        for (var i = 0; i < list.length; i++) {
                            html += _this.temp(list[i]);
                        }

                        pageIndex == 1 ? _this.box.html(html) : $(html).appendTo(_this.box);
                        
                        _this.currentIndex = _this.data.pageIndex;
                        _this.currentCount = _this.data.pageIndex * _this.data.pageSize;

                        typeof(func) == "function" && func();
         
                        typeof(_this.fn) == "function" && _this.fn();

                    }else{
                        _this.isLoaded = true;
                        noDataTemp();
                        typeof(func) == "function" && func();
                    }
                },
                error:function(){
                    _this.isLoaded = true;
                    noDataTemp();
                    typeof(func) == "function" && func();
                }
            });
 

            function noDataTemp(){
                if (pageIndex == 1){
                    $(".dt-loading").hide();
                    $(".loadNone").hide();
                    $(".noneData").show();
                    _this.box.html("");
                }else{
                    $(".dt-loading").hide();
                    $(".loadNone").show();
                    $(".noneData").hide();
                }
            }

            function hide(){
                $(".dt-loading").hide();
                $(".loadNone").hide();
                $(".noneData").hide();
            }
        }
    }


    window.dataLoader = dataloader;
})()